import TableData from "@/components/public/el-table-pagination"; //主页面列表
import TableLyout from '@/components/commonPublic/tableLyoutDetail.vue'
import CustomDialog from "@/components/testDetectionManage/InspectionFormText/index.vue";
import CustomDialog1 from "@/components/testDetectionManage/InspectionForm/index.vue";


import formJs from "./regexDetail.js"; //新增弹框配置
import {
    getInspectionFormWebList, //列表
    submitInspectionForm, //提交
} from "@/api/request.js";
export default {
    name: "InspectionFormText",
    data() {
        return {
            name: "InspectionFormText",
            radio: "待办",
            treelength: true, //左侧树显示隐藏
            btnObj: [], //按钮
            loading: false,
            getList: {
                fCompanyid: "", // 左侧选中组织机构id
                keyWord: "",// 试验名称、报告编号、工程部位/用途
                keyType: "",// 检测类型
                modelType: "0", // 0待办 1已办 2查询
                page: 1, // 当前页码
                pageSize: 20, // 每页显示条数
                userId: "", // 查询待办或已办时传 列表页查询不传
                startDates: "", // 开始时间
                endDates: "", // 截至时间
            },
            queryCriteriaData: {}, //分页
            table_config: formJs.table_config, //列表
            dataFid: null, //列表详情id
            tableData: [],

            // 弹框
            // 详情
            isShow: false, //是否显示弹框
            statusNum: null, //点击那个按钮
            currentRow: null, //列表详情
            dialogTitle: "",
            // 详情

            // 报验单
            isShow1: false, //是否显示弹框
            statusNum1: null, //点击那个按钮
            // 报验单

            // 弹框
            userInfo: {}, //登录用户

        };
    },
    components: {
        TableLyout,
        CustomDialog,//详情
        CustomDialog1,//报验单
        TableData, //主页面列表
    },
    created() { },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("GxBack-userInfo"));
        // console.log("userInfo", this.userInfo);
        this.getList = {
            fCompanyid: this.userInfo.companyId, // 左侧选中组织机构id
            keyWord: "",// 试验名称、报告编号、工程部位/用途
            keyType: "",// 检测类型
            modelType: "0", // 0待办 1已办 2查询
            page: 1, // 当前页码
            pageSize: 20, // 每页显示条数
            userId: this.userInfo.userId, // 查询待办或已办时传 列表页查询不传
            startDates: "", // 开始时间
            endDates: "", // 截至时间
        };

        this.yj_searchForm(this.getList);
        this.btnRole(this.name);
        this.table_config.operation.cols = [
            {
                prop: "fStatus",
                type: "primary",
                handleRow: this.handleRow,
            },
        ];

        this.table_config.operation["specialClass"] = function (rowData) {
            var color = null;
            if (rowData == "待报验") {
                color = "red !important";
            } else if (rowData == "审核中") {
                color = "orange !important";
            } else if (rowData == "已报验") {
                color = "green !important";
            } else {
                color = "#409eff !important";
            }
            return color;
        };
    },
    methods: {
        // 弹框
        refreshList() { //刷新列表
            this.$nextTick(() => {
                this.yj_searchForm(this.getList);
            })
        },
        closeDialog() {
            this.isShow = false;
            this.isShow1 = false;
        },
        // 弹框


        // 待办/已办
        modelType(e) {
            this.currentRow = "";
            this.dataFid = "";
            if (e == "待办") {
                this.getList.modelType = "0";
            } else {
                this.getList.modelType = "1";
            }
            this.yj_searchForm(this.getList);
        },

        // 列表操作按钮
        handleRow() { },


        // 获取列表数据
        yj_searchForm(id) {
            this.loading = true;
            getInspectionFormWebList(id)
                .then((res) => {
                    if (res.errcode == 0) {
                        var that = this;
                        that.loading = false;
                        that.tableData = res.data.pageInfo.list;
                        that.queryCriteriaData = res.data.pageInfo;
                    } else {
                        this.$message.error("列表", res.msg);
                        this.loading = false;
                    }
                })
                .catch(() => {
                    this.loading = false;
                    console.log("获取列表数据接口报错");
                });
        },


        // 导航栏-右侧按钮
        btnRole(name) {
            if (this.userInfo.userId != 'System') {
                var btnList = [];
                var ModuleList = JSON.parse(sessionStorage.getItem("GxBack-ModuleList"));
                if (ModuleList.length > 0) {
                    ModuleList.map((item) => {
                        if (item.fEncode == name) {
                            btnList = item.modulebuttonList;
                        }
                    });
                    btnList.map(item => {
                        item['disabled'] = false
                    })
                    this.btnObj = btnList
                }
            } else {
                this.btnObj = formJs.btnObj
            }
        },
        getBtnName(name, status) {
            this.statusNum = status;
            this.yj_edit(status)
        },
        // 导航栏---右侧按钮



        // 新增/编辑
        yj_edit(num) {
            if (this.userInfo.userId != "System") {
                this.statusNum = num;
                if (num == 1) { // 编辑
                    this.dialogTitle = "报验编辑";
                    if (this.dataFid != null) {
                        if (this.currentRow.fStatus == "待报验") {
                            this.editText();
                        } else {
                            this.$message.warning("请选择信息状态为'待报验'的数据！");
                        }
                    } else {
                        this.$message.warning("请先选择需要编辑的数据");
                    }

                } else if (num == 2) { //提交

                    if (this.dataFid != null) {
                        if (this.currentRow.fInspectiondates != "" && this.currentRow.fInspectiondates != null) {
                            this.yj_submit(this.currentRow);
                        } else {
                            this.$message.warning("本条数据未编辑,不可提交！");
                        }

                    } else {
                        this.$message.warning("请先选择需要提交的数据");
                    }

                } else if (num == 3) {//审核

                    this.dialogTitle = "专监审核";
                    if (this.dataFid != null) {
                        if (this.currentRow.fStatus == "审核中") {
                            this.editText();
                        } else {
                            this.$message.warning("请选择信息状态为'审核中'的数据！");
                        }
                    } else {
                        this.$message.warning("请先选择需要审核的数据");
                    }

                }
            } else {
                this.$message.warning("该用户无权限");
            }
        },
        // 编辑
        editText() {
            this.isShow = true;
        },

        // 提交
        yj_submit(row) {
            if (row.fId != "" && row.fId != null) {
                if (row.fStatus == "待报验") {
                    this.$confirm("是否继续提交?", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    })
                        .then(() => {
                            this.loading = true;
                            submitInspectionForm({
                                fId: row.fId,
                                // userId: this.userInfo.userId,
                                // userName: this.userInfo.realName,
                            })
                                .then((res) => {
                                    if (res.errcode == 0) {
                                        this.loading = false;
                                        this.yj_searchForm(this.getList);
                                        this.$message.success("提交成功");
                                    } else {
                                        this.$message.error("提交失败", res.msg);
                                        this.loading = false;
                                    }
                                })
                                .catch(() => {
                                    console.log("提交失败");
                                    this.loading = false;
                                });
                        })
                        .catch(() => {
                            this.$message("取消提交");
                        });
                } else {
                    return this.$message.error('请选择样品状态为"待取样"的数据！');
                }
            } else {
                return this.$message.error("请先选择需要提交的数据");
            }
        },

        // 点击查看
        yj_viewForm(row, label) {
            if (label == "报验单编号") {
                this.statusNum1 = 7;
                this.currentRow = row;
                this.isShow1 = true;
            }

            if (label == "样品编号") {
                this.dialogTitle = "查看";
                this.statusNum = 7;
                this.currentRow = row;
                this.isShow = true;
            }

            if (label == "报告编号") {
                if (row.fReporturl != "" && row.fReporturl != null && row.fReporturl != "/") {
                    window.open(row.fReporturl);
                } else {
                    this.$message.warning("试验报告暂未出具");

                }
            }

            if (label == "抽检报告编号") {
                if (row.fReportjlurl != "" && row.fReportjlurl != null && row.fReportjlurl != "/") {
                    window.open(row.fReportjlurl);
                } else {
                    this.$message.warning("试验报告暂未出具");

                }
            }
        },
        // 点击当前行变色
        rowChange(val) {
            this.currentRow = val
            this.dataFid = val.fId;
        },



    },
};